/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frmwrk.core;

import bank.entries.AddInterest;
import frmwrk.interfaces.IAccount;
import frmwrk.interfaces.IEntry;
import frmwrk.interfaces.IParty;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Observer;

/**
 *
 * @author Damian
 */
public class FinancialFramework {
    
    protected HashMap<Integer, IAccount> accountMap = new HashMap<Integer, IAccount>();
    protected HashMap<String, IParty> partyMap = new HashMap<String, IParty>();

    public Collection<IAccount> getAccounts() {
        return accountMap.values();
    }
    
    public boolean isNewParty(String name){
        if (partyMap.get(name) == null)
            return true;
        return false;
    }
    
    public boolean addAccountEntry(IAccount account, IEntry entry){
        entry.setPreviousBalance(account.getBalance());
        return account.addEntry(entry);
    }
        
   public void addEntryForAllAccounts(IEntry fixentry){
        for(IAccount account : getAccounts()){
            IEntry entry = new Entry(fixentry.getAmount(),fixentry.getDate(),account);
            addAccountEntry(account, entry);
        }
    }
    
    public IAccount getAccount(int accountNumber){
        return accountMap.get(accountNumber);
                
    }
            
    public void addToAccountMap(IAccount acc){
        
        accountMap.put(acc.getAccountNumber(), acc);
    }
    
    public void addToPartyMap(IParty party){
        
        partyMap.put(party.getName(), party);
    }
    
    public void createAccount(HashMap<String,String> partyParams,HashMap<String,String> accParams, List<Observer> observers,AccountFactory accFactory, CustomerFactory custFactory){
        
        IParty party;
        String partyName = partyParams.get("name");
        if (isNewParty(partyName)){            
            party = custFactory.create(partyParams);
            addToPartyMap(party);
        } else {
            party = partyMap.get(partyName);
            System.out.println("Party was already in system");
        }
        
        Account acc = accFactory.create(accParams, party, observers);
        addToAccountMap(acc);
    }

}
